package com.cx.androidtools;

import android.app.Activity;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.animation.Animation;
import android.view.animation.RotateAnimation;
import android.view.animation.Animation.AnimationListener;
import android.view.Window;
import android.widget.ImageView;

public class ShakeTestActivity extends Activity implements OnClickListener {

	ImageView img_l;
	ImageView img_o;
	ImageView img_a;
	ImageView img_g;
	ImageView img_i;
	ImageView img_n;
	private boolean mNeedShake = false;
	private boolean mStartShake = false;

	private static final int ICON_WIDTH = 40;
	private static final int ICON_HEIGHT = 40;
	private static final float DEGREE_0 = 1.8f;
	private static final float DEGREE_1 = -2.0f;
	private static final float DEGREE_2 = 2.0f;
	private static final float DEGREE_3 = -1.5f;
	private static final float DEGREE_4 = 1.5f;
	private static final int ANIMATION_DURATION = 80;

	private int mCount = 0;

	float mDensity;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.shake_test_layout);
		DisplayMetrics dm = new DisplayMetrics();
		getWindowManager().getDefaultDisplay().getMetrics(dm);
		if (dm != null) {
			mDensity = dm.density;
		}
		img_l = (ImageView) findViewById(R.id.img_l);
		img_l.setOnClickListener(this);
		img_o = (ImageView) findViewById(R.id.img_o);
		img_o.setOnClickListener(this);
		img_a = (ImageView) findViewById(R.id.img_a);
		img_a.setOnClickListener(this);
		img_g = (ImageView) findViewById(R.id.img_g);
		img_g.setOnClickListener(this);
		img_i = (ImageView) findViewById(R.id.img_i);
		img_i.setOnClickListener(this);
		img_n = (ImageView) findViewById(R.id.img_n);
		img_n.setOnClickListener(this);
	}

	@Override
	public void onClick(View v) {
//		switch (v.getId()) {
//		case R.id.img_a:
			if (!mStartShake) {
				mStartShake = true;
				mNeedShake = true;
				shakeAnimation(img_l);
				shakeAnimation(img_o);
				shakeAnimation(img_a);
				shakeAnimation(img_g);
				shakeAnimation(img_i);
				shakeAnimation(img_n);
			}
//			break;
//
//		default:
//			break;
//		}

	}
    private void shakeAnimation(final View v) {
        float rotate = 0;
        rotate=DEGREE_0;
        int c = mCount++ % 5;
        if (c == 0) {
            rotate = DEGREE_0;
        } else if (c == 1) {
            rotate = DEGREE_1;
        } else if (c == 2) {
            rotate = DEGREE_2;
        } else if (c == 3) {
            rotate = DEGREE_3;
        } else {
            rotate = DEGREE_4;
        }
        final RotateAnimation mra = new RotateAnimation(rotate, -rotate, ICON_WIDTH * mDensity / 2, ICON_HEIGHT * mDensity / 2);
        final RotateAnimation mrb = new RotateAnimation(-rotate, rotate, ICON_WIDTH * mDensity / 2, ICON_HEIGHT * mDensity / 2);

        mra.setDuration(ANIMATION_DURATION);
        mrb.setDuration(ANIMATION_DURATION);

        mra.setAnimationListener(new AnimationListener() {
            @Override
            public void onAnimationEnd(Animation animation) {
                if (mNeedShake) {
                    mra.reset();
                    v.startAnimation(mrb);
                }
            }

            @Override
            public void onAnimationRepeat(Animation animation) {

            }

            @Override
            public void onAnimationStart(Animation animation) {

            }

        });

        mrb.setAnimationListener(new AnimationListener() {
            @Override
            public void onAnimationEnd(Animation animation) {
                if (mNeedShake) {
                    mrb.reset();
                    v.startAnimation(mra);
                }
            }

            @Override
            public void onAnimationRepeat(Animation animation) {

            }

            @Override
            public void onAnimationStart(Animation animation) {

            }

        });
        v.startAnimation(mra);
    }

	  @Override
	    public void onBackPressed() {
	        if (!mNeedShake) {
	            super.onBackPressed();
	        } else {
	            mNeedShake = false;
	            mCount = 0;
	            mStartShake = false;
	        }
	    }
}
